<template>
  <div>
    <el-form ref="queryForm" :model="page.query" inline label-width="100px" label-position="right">
<!--      <el-form-item label="字典项编码" prop="itemKey">
        <el-input v-model="page.query.itemKey" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典项名称" prop="itemName">
        <el-input v-model="page.query.itemName" placeholder="请输入字典项名称" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <dict-select v-model="page.query.state" dict-key="comm_state" placeholder="请选择状态" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
      </el-form-item>-->
    </el-form>
    <div class="table-button">
      <el-button icon="el-icon-plus" type="primary" @click="saveClick">新增</el-button>
      <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
      <el-tooltip content="刷新" class="table-right-btn">
        <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
      </el-tooltip>
    </div>
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45" />
      <el-table-column label="材料名称" prop="materialName" />
      <el-table-column label="数量" prop="quantity" />
      <el-table-column label="单位" prop="unit">
        <template slot-scope="scope">
          <dict-table-column v-model="scope.row.unit" dict-key="bom_unit" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="editClick(scope.row)"
          >修改
          </el-button>
          <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='取消'
            placement="top"
            style="margin-left: 10px"
            title="是否删除此条数据"
            @confirm="handleDelConfirm(scope.row)"
          >
            <el-button slot="reference" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { deleteBom, selectBomPage } from '@/api/erp/bom'
import SubmitModule from '@/views/erp/product/bom/modules/submitModule.vue'

export default {
  name: 'DictItemSubmitModule',
  components: { SubmitModule },
  data() {
    return {
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
        }
      },
      tableData: [],
      tableLoading: false,
      selected: [],
      dialogVisible: false,
      productId: ''
    }
  },
  methods: {
    init(productId) {
      this.productId = productId
      this.page.query.productId = productId
      this.doPage()
    },
    doPage() {
      this.tableLoading = true
      selectBomPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(this.productId, null)
      })
    },
    editClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(this.productId, row.id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deleteBom(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.doPage()
    },
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.queryForm.resetFields()
      this.doPage()
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteBom(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
